<template>
  <div class="home-container">
    <!-- 统计卡片区域 -->
    <el-row :gutter="20" class="stats-section">
      <el-col v-for="(stat, index) in statisticData" :key="index" :span="6">
        <el-card shadow="hover" class="stat-card">
          <div class="stat-content">
            <div class="stat-icon" :style="{ backgroundColor: stat.color }">
              <el-icon :size="24" :color="'white'">
                <component :is="stat.icon" />
              </el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-title">{{ $t(stat.titleKey) }}</div>
              <div class="stat-value">{{ stat.value }}</div>
              <div class="stat-trend" :class="stat.trend">
                <el-icon :size="12">
                  <component :is="stat.trendIcon" />
                </el-icon>
                {{ stat.trendText }}
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 快速操作区域 -->
    <el-row :gutter="20" style="margin-top: 20px;">
      <el-col :span="24">
        <el-card shadow="hover" class="quick-actions-card">
          <template #header>
            <div class="card-header">
              <el-icon><Operation /></el-icon>
              <span>{{ $t('quickActions') }}</span>
            </div>
          </template>
          <div class="quick-actions">
            <div v-for="action in quickActions" :key="action.key" class="action-item" @click="handleQuickAction(action.key)">
              <div class="action-icon" :style="{ backgroundColor: action.color }">
                <el-icon :size="20" color="white">
                  <component :is="action.icon" />
                </el-icon>
              </div>
              <div class="action-text">{{ $t(action.titleKey) }}</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 数据概览区域 -->
    <el-row :gutter="20" style="margin-top: 20px;">
      <!-- 任务完成趋势 -->
      <el-col :span="12">
        <el-card shadow="hover" class="chart-card">
          <template #header>
            <div class="card-header">
              <el-icon><TrendCharts /></el-icon>
              <span>{{ $t('taskCompletionTrend') }}</span>
            </div>
          </template>
          <div ref="taskTrendChart" class="chart-container"></div>
        </el-card>
      </el-col>

      <!-- 志愿者活跃度 -->
      <el-col :span="12">
        <el-card shadow="hover" class="chart-card">
          <template #header>
            <div class="card-header">
              <el-icon><PieChart /></el-icon>
              <span>{{ $t('volunteerActivity') }}</span>
            </div>
          </template>
          <div ref="volunteerChart" class="chart-container"></div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 最新动态区域 -->
    <el-row :gutter="20" style="margin-top: 20px;">
      <!-- 最新任务 -->
      <el-col :span="12">
        <el-card shadow="hover" class="news-card">
          <template #header>
            <div class="card-header">
              <el-icon><List /></el-icon>
              <span>{{ $t('latestTasks') }}</span>
              <el-button type="text" size="small" style="margin-left: auto;">{{ $t('viewMore') }}</el-button>
            </div>
          </template>
          <div class="news-list">
            <div v-for="task in latestTasks" :key="task.id" class="news-item">
              <div class="news-avatar">
                <el-avatar :size="40" :src="task.avatar" />
              </div>
              <div class="news-content">
                <div class="news-title">{{ task.title }}</div>
                <div class="news-meta">
                  <span class="news-author">{{ task.requester }}</span>
                  <span class="news-time">{{ task.time }}</span>
                  <el-tag :type="task.statusType" size="small">{{ $t(task.status) }}</el-tag>
                </div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>

      <!-- 社区公告 -->
      <el-col :span="12">
        <el-card shadow="hover" class="news-card">
          <template #header>
            <div class="card-header">
              <el-icon><Bell /></el-icon>
              <span>{{ $t('communityAnnouncements') }}</span>
              <el-button type="text" size="small" style="margin-left: auto;">{{ $t('viewMore') }}</el-button>
            </div>
          </template>
          <div class="news-list">
            <div v-for="announcement in announcements" :key="announcement.id" class="news-item">
              <div class="news-icon" :style="{ backgroundColor: announcement.color }">
                <el-icon color="white">
                  <component :is="announcement.icon" />
                </el-icon>
              </div>
              <div class="news-content">
                <div class="news-title">{{ announcement.title }}</div>
                <div class="news-meta">
                  <span class="news-time">{{ announcement.time }}</span>
                  <el-tag :type="announcement.type" size="small">{{ $t(announcement.category) }}</el-tag>
                </div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 服务热线 -->
    <el-row :gutter="20" style="margin-top: 20px;">
      <el-col :span="24">
        <el-card shadow="hover" class="service-card">
          <div class="service-content">
            <div class="service-info">
              <h3>{{ $t('serviceHotline') }}</h3>
              <p>{{ $t('serviceDescription') }}</p>
              <div class="contact-info">
                <div class="contact-item">
                  <el-icon><Phone /></el-icon>
                  <span>400-123-4567</span>
                </div>
                <div class="contact-item">
                  <el-icon><Message /></el-icon>
                  <span>service@community.com</span>
                </div>
                <div class="contact-item">
                  <el-icon><Clock /></el-icon>
                  <span>{{ $t('serviceTime') }}</span>
                </div>
              </div>
            </div>
            <div class="service-image">
              <el-icon :size="80" color="#409eff"><Service /></el-icon>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import * as echarts from 'echarts';
import {
  User, Document, Check, UserFilled, Operation, TrendCharts, PieChart,
  List, Bell, Phone, Message, Clock, Service, ArrowUp, ArrowDown,
  Plus, Search, Edit, Setting, Warning, SuccessFilled
} from '@element-plus/icons-vue';

const { t } = useI18n();
const router = useRouter();

// 统计数据
const statisticData = ref([
  {
    titleKey: 'registerVolunteer',
    value: 1234,
    color: '#409eff',
    icon: User,
    trend: 'up',
    trendIcon: ArrowUp,
    trendText: '+12%'
  },
  {
    titleKey: 'inspectingTask',
    value: 56,
    color: '#e6a23c',
    icon: Document,
    trend: 'down',
    trendIcon: ArrowDown,
    trendText: '-3%'
  },
  {
    titleKey: 'acceptingTask',
    value: 189,
    color: '#67c23a',
    icon: Check,
    trend: 'up',
    trendIcon: ArrowUp,
    trendText: '+8%'
  },
  {
    titleKey: 'elderlyCount',
    value: 2567,
    color: '#f56c6c',
    icon: UserFilled,
    trend: 'up',
    trendIcon: ArrowUp,
    trendText: '+5%'
  }
]);

// 快速操作
const quickActions = ref([
  { key: 'addVolunteer', titleKey: 'addVolunteer', icon: Plus, color: '#409eff' },
  { key: 'searchTask', titleKey: 'searchTask', icon: Search, color: '#67c23a' },
  { key: 'publishTask', titleKey: 'publishTask', icon: Edit, color: '#e6a23c' },
  { key: 'systemSettings', titleKey: 'systemSettings', icon: Setting, color: '#909399' }
]);

// 最新任务
const latestTasks = ref([
  {
    id: 1,
    title: '陪同李奶奶去医院复查',
    requester: '李奶奶',
    time: '2小时前',
    status: 'pending',
    statusType: 'warning',
    avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
  },
  {
    id: 2,
    title: '帮助王爷爷购买生活用品',
    requester: '王爷爷',
    time: '4小时前',
    status: 'inProgress',
    statusType: 'primary',
    avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
  },
  {
    id: 3,
    title: '协助张奶奶打扫卫生',
    requester: '张奶奶',
    time: '6小时前',
    status: 'completed',
    statusType: 'success',
    avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
  }
]);

// 社区公告
const announcements = ref([
  {
    id: 1,
    title: '社区志愿者培训活动通知',
    time: '1天前',
    category: 'training',
    type: 'primary',
    icon: SuccessFilled,
    color: '#409eff'
  },
  {
    id: 2,
    title: '春节期间服务安排调整',
    time: '2天前',
    category: 'notice',
    type: 'warning',
    icon: Warning,
    color: '#e6a23c'
  },
  {
    id: 3,
    title: '新增健康检测服务项目',
    time: '3天前',
    category: 'service',
    type: 'success',
    icon: Plus,
    color: '#67c23a'
  }
]);

const taskTrendChart = ref(null);
const volunteerChart = ref(null);

// 快速操作处理
const handleQuickAction = (key) => {
  switch (key) {
    case 'addVolunteer':
      const volunteerMenu = {
        name: 'volunteerContorl', // 确保与路由配置一致
        path: '/userContorl/volunteer',
        titleKey: 'volunteerManagement'
      };
      router.push({ name: volunteerMenu.name });
      store.selectMenu(volunteerMenu);
      break;
    case 'searchTask':
      const taskSelectMenu = {
        name: 'taskSelect',
        path: '/task/taskSelect',
        titleKey: 'taskSelect'
      };
      router.push({ name: taskSelectMenu.name });
      store.selectMenu(taskSelectMenu);
      break;
    case 'publishTask':
      const taskCheckMenu = {
        name: 'taskCheck',
        path: '/task/taskCheck',
        titleKey: 'taskCheck'
      };
      router.push({ name: taskCheckMenu.name });
      store.selectMenu(taskCheckMenu);
      break;
    case 'systemSettings':
      // 系统设置逻辑
      break;
  }
};

// 初始化图表
const initCharts = () => {
  // 任务完成趋势图
  const taskChart = echarts.init(taskTrendChart.value);
  const taskOption = {
    tooltip: { trigger: 'axis' },
    xAxis: {
      type: 'category',
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: { type: 'value' },
    series: [{
      data: [12, 19, 15, 22, 18, 25, 20],
      type: 'line',
      smooth: true,
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: 'rgba(64, 158, 255, 0.3)' },
          { offset: 1, color: 'rgba(64, 158, 255, 0.1)' }
        ])
      },
      lineStyle: { color: '#409eff' },
      itemStyle: { color: '#409eff' }
    }]
  };
  taskChart.setOption(taskOption);

  // 志愿者活跃度饼图
  const volunteerChartInstance = echarts.init(volunteerChart.value);
  const volunteerOption = {
    tooltip: { trigger: 'item' },
    series: [{
      type: 'pie',
      radius: ['40%', '70%'],
      data: [
        { value: 335, name: '高度活跃' },
        { value: 310, name: '中度活跃' },
        { value: 234, name: '低度活跃' },
        { value: 135, name: '不活跃' }
      ],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }]
  };
  volunteerChartInstance.setOption(volunteerOption);

  // 响应式处理
  window.addEventListener('resize', () => {
    taskChart.resize();
    volunteerChartInstance.resize();
  });
};

onMounted(() => {
  nextTick(() => {
    initCharts();
  });
});
</script>

<style scoped>
.home-container {
  padding: 20px;
  background: #f5f7fa;
  min-height: calc(100vh - 120px);
}

/* 统计卡片样式 */
.stat-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.stat-content {
  display: flex;
  align-items: center;
  padding: 10px 0;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
}

.stat-info {
  flex: 1;
}

.stat-title {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 4px;
}

.stat-trend {
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.stat-trend.up {
  color: #67c23a;
}

.stat-trend.down {
  color: #f56c6c;
}

/* 卡片头部样式 */
.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: bold;
  font-size: 16px;
  color: #303133;
}

/* 快速操作样式 */
.quick-actions-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.quick-actions {
  display: flex;
  gap: 20px;
  justify-content: space-around;
}

.action-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.action-item:hover {
  background: #f8f9fa;
  transform: translateY(-2px);
}

.action-icon {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
}

.action-text {
  font-size: 14px;
  color: #303133;
  font-weight: 500;
}

/* 图表卡片样式 */
.chart-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  height: 350px;
}

.chart-container {
  height: 280px;
  width: 100%;
}

/* 动态列表样式 */
.news-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.news-list {
  max-height: 300px;
  overflow-y: auto;
}

.news-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.news-item:last-child {
  border-bottom: none;
}

.news-avatar {
  margin-right: 12px;
}

.news-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
}

.news-content {
  flex: 1;
}

.news-title {
  font-size: 14px;
  color: #303133;
  margin-bottom: 6px;
  font-weight: 500;
}

.news-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 12px;
  color: #909399;
}

/* 服务热线样式 */
.service-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.service-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px;
}

.service-info h3 {
  margin: 0 0 10px 0;
  font-size: 20px;
}

.service-info p {
  margin: 0 0 20px 0;
  opacity: 0.9;
}

.contact-info {
  display: flex;
  gap: 30px;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}

.service-image {
  opacity: 0.3;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .home-container {
    padding: 10px;
  }
  
  .quick-actions {
    flex-wrap: wrap;
    gap: 10px;
  }
  
  .contact-info {
    flex-direction: column;
    gap: 10px;
  }
  
  .service-content {
    flex-direction: column;
    text-align: center;
  }
}
</style>
